package com.bi.onlineEducation.web.controller;

import com.bi.onlineEducation.annotation.WebProcess;
import com.bi.onlineEducation.dao.LoginDao;
import com.bi.onlineEducation.domain.LoginPo;
import com.bi.onlineEducation.exception.BusinessHandleException;
import com.bi.onlineEducation.service.LoginService;
import com.bi.onlineEducation.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: chenYuTao
 * @Time: 2022/1/6 15:44
 * @Description: 基础数据控制层
 **/
@RestController
@Api(tags = "用户登录控制层", description = "用户登录数据接口")
public class LoginController {

    @Resource
    private LoginService loginService;
    @Resource
    private LoginDao loginDao;

    @ApiOperation("查询所有")
    @WebProcess
    @GetMapping(value = "/login/findAll")
    public ResultCode findAll() {
        return ResultCode.success(loginService.findAll());
    }

    @ApiOperation("根据role查询所有")
    @WebProcess
    @GetMapping(value = "/login/findAllByRole")
    public ResultCode findAllByRole(@RequestParam("role") Integer role) {
        return ResultCode.success(loginService.findAllByRole(role));
    }

    @ApiOperation("根据id查询登录信息")
    @WebProcess
    @GetMapping(value = "/login/findById")
    public ResultCode findById(@RequestParam("id") Integer id) {
        return ResultCode.success(loginService.findById(id));
    }

    @ApiOperation("教师、普通用户搜索")
    @WebProcess
    @GetMapping(value = "/login/searchByRole")
    public ResultCode searchByRole2(@RequestParam("realName") String realName,@RequestParam("address") String address
            ,@RequestParam("role") Integer role) {
        return ResultCode.success(loginService.searchByRole(realName,address,role));
    }

    @ApiOperation("新增账户信息")
    @WebProcess
    @PostMapping(value = "/login/add")
    public ResultCode add(@RequestBody LoginPo loginPo) {
        if (ObjectUtils.isNotEmpty(loginDao.findByLoginName(loginPo.getLoginName()))) {
            throw BusinessHandleException.buildBizException("已存在该账户");
        }
        loginPo.setGender(StringUtils.equals("女", loginPo.getGender()) ? "1" : "2");
        loginPo.setBalance(BigDecimal.valueOf(6000));
        return ResultCode.success(loginService.save(loginPo));
    }

    @ApiOperation("保存账户信息")
    @WebProcess
    @PostMapping(value = "/login/save")
    public ResultCode save(@RequestBody LoginPo loginPo) {
        return ResultCode.success(loginService.save(loginPo));
    }

    @ApiOperation("根据id删除登录信息")
    @WebProcess
    @DeleteMapping(value = "/login/deleteById")
    public ResultCode deleteById(@RequestParam("id") Integer id) {
        return ResultCode.success(loginService.deleteById(id));
    }

    @ApiOperation("批量根据id删除登录信息")
    @WebProcess
    @DeleteMapping(value = "/login/deleteByIds")
    public ResultCode deleteById(@RequestParam("ids") List<Integer> ids) {
        return ResultCode.success(loginService.deleteByIds(ids));
    }

    @ApiOperation("用户账户密码登录")
    @WebProcess
    @PostMapping(value = "/login")
    public ResultCode login(@RequestBody LoginPo loginPo) {
        return ResultCode.success(loginService.login(loginPo.getLoginName(), loginPo.getPassword()));
    }


}